package com.boy.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boy.entity.Role;
import com.boy.service.RoleService;
import com.boy.tool.domain.Pos;
import com.boy.tool.annotate.RequirePermissions;
import com.boy.tool.domain.ResultVo;
import com.boy.tool.annotate.WhdLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author whd
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @WhdLog
    @RequirePermissions({"Role:create"})
    @PostMapping("/create")
    public ResultVo create(@RequestBody Role role) {
        if (roleService.save(role)) return ResultVo.success(role);
        return ResultVo.failure();
    }

    @RequirePermissions({"Role:get"})
    @PostMapping("/get")
    public ResultVo get(@RequestParam String id) {
        Role role = roleService.getById(id);
        if (role == null) return ResultVo.failure();
        return ResultVo.success(role);
    }

    @WhdLog
    @RequirePermissions({"Role:delete"})
    @PostMapping("/delete")
    public ResultVo delete(@RequestParam String id) {
        Boolean role = roleService.removeById(id);
        if (!role) return ResultVo.failure();

        return ResultVo.success(role);
    }

    @RequirePermissions({"Role:find"})
    @PostMapping("/find")
    public ResultVo find(@RequestBody Pos pos) {
        IPage<Role> roleIPage = roleService.find(pos);
        return ResultVo.success(roleIPage);
    }

    @WhdLog
    @RequirePermissions({"Role:update"})
    @PostMapping("/update")
    public ResultVo update(@RequestBody Role role) {
        if (roleService.updateById(role)) return ResultVo.success(role);
        return ResultVo.failure();
    }
}

